﻿#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System;
using System.Diagnostics;


namespace NUnitGrid.Framework.Logging
{
    public class PerformanceLogger
    {
        private readonly ILogger logger;
        private readonly string name;
        private readonly TimeSpan reportPeriod;
        private readonly Stopwatch reportTimer = new Stopwatch();
        private readonly Stopwatch scopeTotalTime = new Stopwatch();
        private readonly Stopwatch totalTime = new Stopwatch();
        private int measurmentsCount;

        public PerformanceLogger(string name, TimeSpan reportPeriod, ILogger logger)
        {
            this.name = name;
            this.reportPeriod = reportPeriod;
            this.logger = logger;
        }

        public PerformanceScope Measure
        {
            get { return new PerformanceScope(this); }
        }

        public void Start()
        {
        }

        internal void StartMeasurement()
        {
            scopeTotalTime.Start();
            if (!totalTime.IsRunning)
            {
                totalTime.Start();
            }

            if (!reportTimer.IsRunning)
            {
                reportTimer.Start();
            }
        }

        internal void EndMeasurement()
        {
            scopeTotalTime.Stop();
            measurmentsCount++;

            if (reportTimer.Elapsed >= reportPeriod)
            {
                reportTimer.Stop();
                reportTimer.Reset();

                totalTime.Stop();

                logger.Info.Write("{0}: {1:###.0}%. {2} measurments",
                                  name,
                                  (scopeTotalTime.ElapsedMilliseconds*100.0)/totalTime.ElapsedMilliseconds,
                                  measurmentsCount);

                totalTime.Reset();
                totalTime.Start();

                scopeTotalTime.Reset();

                measurmentsCount = 0;
            }
        }

        public class PerformanceScope : IDisposable
        {
            private readonly PerformanceLogger parent;

            internal PerformanceScope(PerformanceLogger parent)
            {
                this.parent = parent;
                parent.StartMeasurement();
            }

            public void Dispose()
            {
                parent.EndMeasurement();
            }
        }
    }
}